package workshopdesign.Control;

import java.util.Collection;
import java.util.HashMap;

import workshopdesign.Model.Person;

/**
 *
 * @author Team1 @ WorkshopDesign
 */
public class AddressController {

    private HashMap<Long, Person> personList;
    private long next_id = 0;
    private static AddressController addressController;

    public AddressController(/*---*/) {
        personList = new HashMap<Long, Person>();

    }

    public static AddressController getAddressController() {
        if (addressController == null) {
            addressController = new AddressController();
        }
        return addressController;
    }

    public static HashMap<Long, Person> save(){
        return new HashMap<Long, Person>(getAddressController().personList);
    }

    public static long save_id(){
        return getAddressController().next_id;
    }

    public static void load(HashMap<Long, Person> person_list, long next_id){
        getAddressController().personList = new HashMap<Long, Person>(person_list);
        getAddressController().next_id = next_id;
    }

    public void createPerson(String name, String address, String postalCode, String city, String phone, String email) {
        Person person = new Person(next_id, name, address, postalCode, city, phone, email);
        personList.put(next_id, person);
        next_id++;
    }

    public void deletePerson(long id) {
        personList.remove(id);
    }

    public void updatePerson(long id, String name, String address, String postalCode, String city, String phone, String email) {
        Person person = personList.get(id);
        person.changeName(name);
        person.changeAddress(address);
        person.changePostalCode(postalCode);
        person.changeCity(city);
        person.changePhone(phone);
        person.changeEmail(email);
    }

    public Collection<Person> listAllPersons() {
        return personList.values();
    }

    public Person getPerson(long id) {
        Person person = personList.get(id);
        return person;
    }
}

